function require() {
	this.requestMethods = (method = "GET", url = "", data = {}) => {
		return new Promise((res, rej) => {
			uni.request({
				url: url,
				method: method || "GET",
				data,
				header: {
					Authorization: uni.getStorageSync("token") ? "Bearer " + uni.getStorageSync("token") : "",
				},
				success(e) {
					if (e.statusCode === 200) {
						if (e.data.code === 0) {
							res(e.data.data)
						} else {
							if (e.data.code === 401 || e.data.code === 1000) {
								uni.showToast({
									title: "登录失效,请重新登录",
									icon: "none",
								})
								uni.clearStorageSync()
								uni.redirectTo({
									url: "/pages/login/index",
								})
							}
							rej(e)
						}
					}
				},
				fail(e) {
					rej(e)
				},
			})
		})
	}
}

const http = new require()

function request(method = "get", url = "", data = {}) {
	method = method.toUpperCase()
	if (method === "GET") {
		return http.requestMethods("GET", url, data)
	}
	if (method === "POST") {
		return http.requestMethods("POST", url, data)
	}
	if (method === "PUT") {
		return http.requestMethods("PUT", url, data)
	}
	if (method === "DELEDE") {
		return http.requestMethods("DELEDE", url, data)
	}
}

export default request
